from kubernetes import client,config
import os,yaml,json
from datetime import date, timedelta
apiserver = "https://192.168.0.11:6443"
token = 'eyJhbGciOiJSUzI1NiIsImtpZCI6IlBvcjhjTS1wZG9LdlFMcm1nc01hajBnLUhFaGpWTzZOZ0ZKMHE1LWxnMEEifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4taHg4a2oiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiNGUxNjVlY2ItZmI2NS00OTM4LThmMmMtOWVhYWQ0NDI0NjhkIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.DVv4k66NmnTbaxy5RaKk1tpS2aoB_k5IoMpwGZQfbj724z-63hypk2rFmcEOji8vej4Kl-BLFoXEIP5oINFWi6EgEA7UjM9yz0uJSgXfOGRAaup-j9-yQs0bt-AdHrQ-8s032VbpGykk880Gdry2e5c2kgmHz1fPyz6MP4EvU3Wf1nnJmh_7hZdfBf-hD6jtyWRJzXNk-RRpPH_7cJ-RpuOES8f2ZZowlUxB1_Mqqt6i83CFXrphIEFMyn9bGhqWyIi0U_qMQbjxlDm_77mnnvh_c-PUMGVFHtMAv0907SCOkbSIVV7iu9XE6CybFk83gFSKzW4xgk7e0NW1dlwEfQ'

# 加载连接k8s api的认证配置
def load_auth_config():
    configuration = client.Configuration()
    configuration.host = apiserver  # APISERVER地址
    # ca_file = os.path.join(os.getcwd(), "ca.crt")  # K8s集群CA证书（/etc/kubernetes/pki/ca.crt）
    # configuration.ssl_ca_cert = ca_file
    configuration.verify_ssl = False  # 关闭证书验证
    configuration.api_key = {"authorization": "Bearer " + token}
    client.Configuration.set_default(configuration)

# 资源创建时间格式化
def timestamp_format(timestamp):
    new_date = timestamp + timedelta(hours=8)
    new_date_format = date.strftime(new_date, '%Y-%m-%d %H:%M:%S')
    return new_date_format


def bytes_to_yaml(content):
    json_obj = json.loads(content.decode())
    yaml_obj = yaml.safe_dump(json_obj)
    return yaml_obj